﻿using SuYunExpressPackageSystem.BLL;
using SuYunExpressPackageSystem.Models;
using SuYunExpressPackageSystem.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace SuYunExpressPackageSystem
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private MenuBLL menuBLL = new MenuBLL();
        private string loginName = "";//登录名
        private System.Timers.Timer timerDT = null;//定时器

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //初始化处理
            InitMainPageInfo();
            picClose.Visible = true;
        }

        private void InitMainPageInfo()
        {
            //加载登录信息
            if (this.Tag != null)
                loginName = this.Tag.ToString();
            if (!string.IsNullOrEmpty(loginName))
                lblLoginInfo.Text = loginName + "，欢迎使用系统！";
            else
            {
                MessageHelper.Error("系统加载", "你没有登录系统！");
                return;
            }

            lblBanQuan.Text = "";

            //动态时间
            timerDT = new System.Timers.Timer();
            timerDT.Interval = 1000;
            timerDT.AutoReset = true;
            timerDT.Elapsed += TimerDT_Elapsed;
            lblLoginTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            timerDT.Start();

            //加载菜单栏
            cainiaoMenus.Items.Clear();
            //获取菜单项数据
            List<MenuInfo> allMenuList = menuBLL.GetMenuList();
            //加载菜单项到菜单栏   ---递归方法加载项
            CreateMenuItems(allMenuList, null, 0);
            //默认显示系统导航子页
            tabPages.AddTabFormPage(new FrmNavigation());
            picClose.Visible = true;
            lblAction.Text = "系统导航页";
        }

        private void TimerDT_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.IsHandleCreated)
            {
                this.Invoke(new Action(() =>
                {
                    lblLoginTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }));
            }
        }

        /// <summary>
        /// 动态添加菜单项
        /// </summary>
        /// <param name="allMenus"></param>
        /// <param name="pMenu"></param>
        /// <param name="parentId"></param>
        private void CreateMenuItems(List<MenuInfo> allMenus, ToolStripMenuItem pMenu, int parentId)
        {
            //子菜单列表
            var subItems = allMenus.Where(m => m.ParentId == parentId);
            foreach (MenuInfo menuInfo in subItems)
            {
                //创建菜单项
                ToolStripMenuItem mItem = new ToolStripMenuItem();
                mItem.Name = menuInfo.MenuId.ToString();
                mItem.Text = menuInfo.MenuName;
                if (parentId == 0)
                {
                    //一级菜单
                    mItem.Font = new Font("微软雅黑", 10.8F, FontStyle.Bold, GraphicsUnit.Point);
                    mItem.ForeColor = Color.Navy;
                }
                else
                {
                    //二级菜单
                    mItem.Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point);
                    mItem.ForeColor = Color.Blue;
                }
                //快捷键
                if (!string.IsNullOrEmpty(menuInfo.MKey))
                {
                    string[] keyArr = menuInfo.MKey.Split('+');
                    //长度  2 、3   Ctrl+A   Alt+S  Shift+A    Ctrl+Shift+T
                    if (keyArr.Length == 2)
                    {
                        Keys key = (Keys)Enum.Parse(typeof(Keys), keyArr[1]);
                        if (keyArr[0] == "Alt")
                        {
                            mItem.Text += $"(&{keyArr[1]})";
                            mItem.ShortcutKeys = (Keys.Alt | key);
                        }
                        else if (keyArr[0] == "Ctrl")
                        {
                            mItem.ShortcutKeys = (Keys.Control | key);
                        }
                        else if (keyArr[0] == "Shift")
                        {
                            mItem.ShortcutKeys = (Keys.Shift | key);
                        }
                    }
                    else if (keyArr.Length == 3)
                    {
                        Keys key = (Keys)Enum.Parse(typeof(Keys), keyArr[2]);
                        string twoKeys = keyArr[0] + "+" + keyArr[1];
                        switch (twoKeys)
                        {
                            case "Ctrl+Shift":
                                mItem.ShortcutKeys = (Keys.Control | Keys.Shift | key);
                                break;

                            case "Ctrl+Alt":
                                mItem.ShortcutKeys = (Keys.Control | Keys.Alt | key);
                                break;

                            case "Shift+Alt":
                                mItem.ShortcutKeys = (Keys.Shift | Keys.Alt | key);
                                break;
                        }
                    }
                }
                //页面地址
                if (!string.IsNullOrEmpty(menuInfo.FrmURL))
                {
                    mItem.Tag = menuInfo.FrmURL;//传递页面地址
                }
                //单击事件订阅---无子菜单的项
                if (allMenus.Where(m => m.ParentId == menuInfo.MenuId).Count() == 0)
                {
                    mItem.Click += MItem_Click;
                }

                //菜单项添加在哪一级
                if (pMenu != null)
                    pMenu.DropDownItems.Add(mItem);
                else
                    cainiaoMenus.Items.Add(mItem);
                CreateMenuItems(allMenus, mItem, menuInfo.MenuId);//创建当前菜单项的子菜单
            }
        }

        /// <summary>
        /// 菜单响应事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            if (menuItem.Tag != null)
            {
                string url = menuItem.Tag.ToString();//获取页面名称
                string frmName = url.Split('.')[1];//Form名称
                Form form = FormUtility.GetOpenForm(frmName);//获取当前要打开的窗体是否已经打开
                if (form == null)
                {
                    //创建窗体
                    string spaceName = this.GetType().Namespace;//命名空间名
                    string fullName = spaceName + "." + url;//完整名称
                    Type type = Type.GetType(fullName);//窗体的Type对象
                    form = (Form)Activator.CreateInstance(type);//窗体对象
                }
                //Form对象添加到TabControl中
                tabPages.AddTabFormPage(form);
                CheckPage();
            }
            else
            {
                //退出系统
                if (menuItem.Text.Contains("退出系统"))
                {
                    Application.Exit();//退出应用程序
                }
            }
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageHelper.Confirm("退出系统", "你确定要退出速运快递包裹管理系统吗？") == DialogResult.OK)
            {
                //确定要退出
                timerDT.Stop();
                Application.ExitThread();
            }
            else
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 子页自适应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabPages_SizeChanged(object sender, EventArgs e)
        {
            if (tabPages.TabPages.Count > 0)
            {
                Size size = tabPages.SelectedTab.Size;//子页的尺寸
                foreach (TabPage tabPage in tabPages.TabPages)
                {
                    Control c = tabPage.Controls[0];
                    if (c is Form)
                    {
                        Form form = (Form)c;
                        form.WindowState = FormWindowState.Normal;
                        form.SuspendLayout();//挂起布局
                        form.Size = size;
                        form.ResumeLayout(true);//恢复布局
                        form.WindowState = FormWindowState.Maximized;
                    }
                }
            }
        }

        /// <summary>
        /// 关闭页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picClose_Click(object sender, EventArgs e)
        {
            TabPage tabPage = tabPages.SelectedTab;
            Form frm = (Form)tabPage.Controls[0];
            frm.Close();
            tabPages.TabPages.Remove(tabPage);
            CheckPage();
        }

        private void CheckPage()
        {
            if (tabPages.TabPages.Count == 0)
                picClose.Visible = false;
            else
                picClose.Visible = true;
        }

        private void tabPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabPages.SelectedTab != null)
                lblAction.Text = tabPages.SelectedTab.Text;//当前操作页
        }

        private void picClose_MouseHover(object sender, EventArgs e)
        {
            picClose.BorderStyle = BorderStyle.FixedSingle;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            picClose.BorderStyle = BorderStyle.None;
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 站点信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbtnStation_Click(object sender, EventArgs e)
        {
            tabPages.ShowTabFormPage<BM.FrmStationList>();
        }

        /// <summary>
        /// 货架信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbtnShelves_Click(object sender, EventArgs e)
        {
            tabPages.ShowTabFormPage<BM.FrmShelvesList>();
        }

        /// <summary>
        /// 员工信息管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbtnEmployee_Click(object sender, EventArgs e)
        {
            tabPages.ShowTabFormPage<Employee.FrmEmployeeList>();
        }

        /// <summary>
        /// 快递信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbtnExpress_Click(object sender, EventArgs e)
        {
            tabPages.ShowTabFormPage<Exp.FrmExpressList>();
        }

        /// <summary>
        /// 派送管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbtnDistribute_Click(object sender, EventArgs e)
        {
            tabPages.ShowTabFormPage<Exp.FrmExpressDistribution>();
        }

        /// <summary>
        /// 快递自提
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbtnSelfPick_Click(object sender, EventArgs e)
        {
            tabPages.ShowTabFormPage<Exp.FrmExpSelfPickList>();
        }
    }
}